﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Joyeon.Utils
{
    public static class StringUtil
    {
        /// <summary>
        /// 将一个Object数组中的内容转换为一个用指定连接符连接的字符串
        /// </summary>
        /// <param name="array"></param>
        /// <param name="connector">默认连接符为","</param>
        /// <returns></returns>
        public static String Join(Object[] array, String connector = ",")
        {
            StringBuilder sb = new StringBuilder();

            if (array != null && array.Length > 0)
            {
                int length = array.Length;
                sb.Append(array[0]);
                for (int i = 1; i < length; i++)
                {
                    sb.Append(connector).Append(array[i]);
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 调用list中对象的toString方法，返回由c所代表的字符分隔的字符串
        /// </summary>
        /// <param name="list">源列表</param>
        /// <param name="connector">分隔符</param>
        /// <returns></returns>
        public static String Join(IList<object> list, String connector)
        {
            StringBuilder sb = new StringBuilder();
            if (list != null && list.Count > 0)
            {
                sb.Append(list[0]);
                for (int i = 1; i < list.Count; i++)
                {
                    sb.Append(connector).Append(list[i]);
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 判断一个字符串类型的值是否为Null
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNull(String input) 
        {
            return null == input;
        }

        /// <summary>
        /// 判断一个字符串类型的值是否为空值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsEmpty(String input) 
        {
            return input == String.Empty;
        }

        /// <summary>
        /// 判断一个字符串类型的值是否为空白符符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsWhiteSpace(String input) 
        {
            return !IsNull(input) && String.IsNullOrWhiteSpace(input);
        }

        /// <summary>
        /// 判断一个字符串类型的值是否为Null或者空字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(String input) 
        {
            return String.IsNullOrEmpty(input);
        }

        /// <summary>
        /// 判断一个字符串类型的值是否为Null或者空白符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(String input) 
        {
            return String.IsNullOrWhiteSpace(input);
        }

        /// <summary>
        /// 判断一个字符串类型的值是否为Null或者空字符或者空白符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNullOrEmptyOrWhiteSpace(String input) 
        {
            return String.IsNullOrEmpty(input) || String.IsNullOrWhiteSpace(input);
        }

        /// <summary>
        /// 使用字符pad按字节填充str到指定的字节长度
        /// </summary>
        /// <param name="str"></param>
        /// <param name="pad"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string LeftPadding(string str, char pad, int length)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(str);
            if (bytes.Length >= length)
            {
                return str;
            }
            int spaces = length - bytes.Length;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < spaces; i++)
            {
                sb.Append(pad);
            }
            sb.Append(str);
            return sb.ToString();
        }

        /// <summary>
        /// 使用字符pad按字节填充str到指定的字节长度
        /// </summary>
        /// <param name="str"></param>
        /// <param name="pad"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string RightPadding(string str, char pad, int length)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(str);
            if (bytes.Length >= length)
            {
                return str;
            }
            int spaces = length - bytes.Length;
            StringBuilder sb = new StringBuilder();
            sb.Append(str);
            for (int i = 0; i < spaces; i++)
            {
                sb.Append(pad);
            }
            return sb.ToString();
        }
    }
}
